<template>
  <div v-loading="loading" class="permission">
    <el-card>
      <div style="text-align: right">
        <el-button type="primary" @click="add('0', 1)">添加菜单</el-button>
      </div>
      <el-table :data="list" row-key="id">
        <el-table-column prop="name" label="菜单名称" />
        <el-table-column prop="code" label="权限标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作" width="300">
          <template v-slot="{ row }">
            <el-button
              type="text"
              @click="add(row.id, 2)"
            >添加权限点</el-button>
            <!-- <el-button type="text">查看api权限</el-button> -->
            <el-button type="text" @click="edit(row)">编辑</el-button>
            <el-button type="text" @click="del(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <Dialog ref="dialog" v-model="isShow" @getData="getData" />
  </div>
</template>
<script>
import { sysPermission, sysPermissionDel } from '@/api/permission.js'
import { changeTreeData } from '@/utils/index'
import Dialog from './dialog.vue'
export default {
  name: '',
  components: { Dialog },
  data() {
    return {
      list: [{ name: 'wang' }],
      loading: false,
      isShow: false
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 获取权限点
    async getData() {
      this.loading = true
      const res = await sysPermission()
      // console.log(res)
      // this.list = this.changeData(res.data, '0')
      this.list = changeTreeData(res.data, '0')
      this.loading = false
    },
    // changeData(arr, str) {
    //   return arr.filter((item) => {
    //     if (item.pid === str) {
    //       item.children = this.changeData(arr, item.id)
    //       return true
    //     }
    //   })
    // },

    // 删除
    del(row) {
      this.$confirm(`确定删除--${row.name}--吗`, '提示')
        .then(async() => {
          await sysPermissionDel(row.id)
          this.getData()
          this.$message.success('删除成功')
        })
        .catch(() => {})
    },
    add(pid, type) {
      this.isShow = true
      this.$refs.dialog.form.type = type
      this.$refs.dialog.form.pid = pid
    },
    edit(row) {
      this.isShow = true
      this.$refs.dialog.mode = 'edit'
      this.$refs.dialog.form = JSON.parse(JSON.stringify(row))
    }
  }
}
</script>
<style scoped lang="scss">
.permission {
  padding: 20px;
}
</style>
